package com.mspbots.cw.reports.impl;

import com.mspbots.cw.amqp.model.Ticket;
import com.mspbots.cw.config.ReportsRabbitConfiguration;
import com.mspbots.cw.reports.CWTicketReportsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
public class CWTicketReportsServiceImpl implements CWTicketReportsService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void cw_ticket_message(Ticket ticket) {
        try {
            if (!Objects.isNull(ticket.getUpdatedBy()) && !Objects.isNull(ticket.getStatus())) {
                Map<String, Object> msg = new ConcurrentHashMap<>();
                msg.put("tenantId", ticket.getTenantId());
                msg.put("identifier", ticket.getUpdatedBy());
                msg.put("cwId", ticket.getCwId());
                msg.put("ticketStatus", ticket.getStatus());
                msg.put("msgType", "CW_ticket");
                msg.put("msgDatetime", LocalDateTime.now().toString());
                rabbitTemplate.convertAndSend(ReportsRabbitConfiguration.EXCHANGE_NAME, ReportsRabbitConfiguration.NTC_CW_TICKET_QUEUE, msg);
            }
        } catch (Exception e) {
        }
    }
}
